import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-09-12
 * Time: 11:32
 */
class Solution3 {
    public List<Integer> largestValues(TreeNode root) {
        Queue<TreeNode> q = new LinkedList<>();
        List<Integer> ret = new ArrayList<>();

        if(root == null) {
            return ret;
        }

        q.add(root);

        while(!q.isEmpty()) {
            int size = q.size();
            int tmp = Integer.MIN_VALUE;
            for(int i = 0; i < size; i++) {

                TreeNode t = q.poll();
                tmp = Math.max(tmp,t.val);

                if(t.left != null) {
                    q.add(t.left);
                }

                if(t.right != null) {
                    q.add(t.right);
                }
            }
            ret.add(tmp);
        }
        return ret;
    }
}
